package com.gilbertoca.igreja.view.security;

import com.gilbertoca.igreja.model.security.Rol;
import com.gilbertoca.igreja.service.security.SeguridadService;
import com.gilbertoca.igreja.view.Application;
import com.gilbertoca.igreja.view.component.Refreshable;
import com.gilbertoca.igreja.view.dialogs.ErrorDialog;
import com.gilbertoca.igreja.view.dialogs.WarningDialog;
import com.gilbertoca.igreja.view.security.models.RolesTableModel;
import java.awt.event.KeyEvent;
import com.gilbertoca.igreja.view.action.ConfigurarAccionesDialog;
import org.jdesktop.application.Action;
import com.gilbertoca.igreja.view.component.Listador;

public class RolesView extends Listador<Rol> implements Refreshable {

    public RolesView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        refresh();
    }
    
    public void refresh() {
        tablaRoles.setModel(new RolesTableModel(SeguridadService.get().getAllRoles()));
    }

    @Action
    public void nuevo() {
        EditarRolView editarRolView = new EditarRolView(Application.getApplication().getMainFrame(), this);
        editarRolView.setLocationRelativeTo(Application.getApplication().getMainFrame());
        Application.getApplication().show(editarRolView);
    }
    
    @Action
    public void cerrar() {
        dispose();
    }
    
    @Action
    public void modificar() {
        int row = tablaRoles.getSelectedRow();
       
        if (row >= 0) {
            Rol rol = ((RolesTableModel)tablaRoles.getModel()).getElements().get(row);
            EditarRolView editarRolView = new EditarRolView(Application.getApplication().getMainFrame(), this,rol);
            editarRolView.setLocationRelativeTo(Application.getApplication().getMainFrame());
            Application.getApplication().show(editarRolView);
       } else {
            WarningDialog.show("Debe seleccionar un rol.");
       }        
    }

    @Action
    public void configurar() {
        int row = tablaRoles.getSelectedRow();

        if (row >= 0) {
            Rol rol = ((RolesTableModel)tablaRoles.getModel()).getElements().get(row);
            ConfigurarAccionesDialog configurarView = new ConfigurarAccionesDialog(Application.getApplication().getMainFrame(), true, rol);
            configurarView.setLocationRelativeTo(Application.getApplication().getMainFrame());
            Application.getApplication().show(configurarView);
       } else {
            WarningDialog.show("Debe seleccionar un rol.");
       }
    }

    @Action
    public void eliminar() {
        boolean eliminado;
        int row = tablaRoles.getSelectedRow();
       
        if (row >= 0) {
            Rol rol = ((RolesTableModel)tablaRoles.getModel()).getElements().get(row);
            try{
                rol.getAcciones().clear();
                SeguridadService.get().update(rol);
                SeguridadService.get().remove(rol);
            }catch(Exception ex){
               ErrorDialog.show("No es posible eliminar el rol.");
            }
       } else {
            WarningDialog.show("Debe seleccionar un rol.");
       }        
    }    
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaRoles = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.gilbertoca.igreja.view.Application.class).getContext().getResourceMap(RolesView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaRoles.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nombre", "Descripción"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaRoles.setName("tablaRoles"); // NOI18N
        tablaRoles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tablaRolesMousePressed(evt);
            }
        });
        tablaRoles.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tablaRolesKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tablaRoles);

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(resourceMap.getColor("jPanel1.border.highlightColor"), resourceMap.getColor("jPanel1.border.shadowColor"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.gilbertoca.igreja.view.Application.class).getContext().getActionMap(RolesView.class, this);
        jButton1.setAction(actionMap.get("nuevo")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setEnabled(Application.getApplication().getUsuario().tieneAccion(Rol.COD_CREAR));
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("modificar")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setEnabled(Application.getApplication().getUsuario().tieneAccion(Rol.COD_MODIFICAR));
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("eliminar")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setEnabled(Application.getApplication().getUsuario().tieneAccion(Rol.COD_ELIMINAR));
        jButton3.setName("jButton3"); // NOI18N

        jButton5.setAction(actionMap.get("cerrar")); // NOI18N
        jButton5.setIcon(resourceMap.getIcon("jButton5.icon")); // NOI18N
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jButton4.setAction(actionMap.get("configurar")); // NOI18N
        jButton4.setIcon(resourceMap.getIcon("jButton4.icon")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setEnabled(Application.getApplication().getUsuario().tieneAccion(Rol.COD_CONFIGURAR));
        jButton4.setName("jButton4"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jButton5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton3)
                .add(41, 41, 41)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 174, Short.MAX_VALUE)
                .add(jButton5)
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(15, 15, 15))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaRolesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaRolesKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            this.seleccionoDeLista();
        }
    }//GEN-LAST:event_tablaRolesKeyPressed

    private void tablaRolesMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaRolesMousePressed
         if(evt.getClickCount() == 2){
            this.seleccionoDeLista();
        }
    }//GEN-LAST:event_tablaRolesMousePressed

    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tablaRoles;
    // End of variables declaration//GEN-END:variables

    @Override
    protected Rol select() {
        int row = tablaRoles.getSelectedRow();
        Rol u = null;
        if (row >= 0) {
             u = ((RolesTableModel) tablaRoles.getModel()).getElements().get(tablaRoles.convertRowIndexToModel(row));

        }else {
            WarningDialog.show("Debe seleccionar un rol.");
        }

        return u;
    }

    @Override
    public void buscar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
